<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\ScoreHandle;
use App\Models\ClassCourse;
use App\Models\Grade;
use App\Models\Score;
use App\Models\Student;
use App\Models\Course;
use App\Models\Semester;
use App\Models\Teacher;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Illuminate\Http\Request;
use Encore\Admin\Facades\Admin;


class TeacherInsertScoreController extends Controller
{

    use HasResourceActions;
    private $id;
    public function __construct()
    {
        $this->id = request('id');
    }

    public function index(Content $content)
    {
        return $content
            ->header('教学管理')
            ->description('成绩录入')
            ->body($this->grid());
    }

    public function show($id, Content $content)
    {
        return $content
            ->header('教学管理')
            ->description('')
            ->body($this->detail($id));

    }

    public function edit($id, Content $content)
    {
        dd($_GET);
        return $content
            ->header('教学管理')
            ->description(' ')
            ->body($this->form()->edit($id));
    }

    public function create(Content $content)
    {

        return $content
            ->header('教学管理')
            ->description(' ')
            ->body($this->form());
    }

    protected function detail($id)
    {
        $show = new Show(ClassCourse::findOrFail($id));

//        $show->name('学期名称');
//        $show->start_date('开始日期');
//        $show->end_date('结束日期');
//        $show->is_semester('Is semester');
//        $show->created_at('Created at');
//        $show->updated_at('Updated at');

        return $show;
    }

    protected function grid()
    {
        $grid = new Grid(new Student);
        $course_arrangement_id = $_GET['id'];
        $total = $_GET['total'];
        $ClassCourse = ClassCourse::where('id',$course_arrangement_id)->first();
        $class_id = $ClassCourse->class_id;

        $grid->model()->where('class_id', $class_id)->where('id', '>', 0);

        $grid->id('Id')->sortable();

        $grid->column('semester_name','学期')->display(function () {
            $course_arrangement_id = $_GET['id'];
            $ClassCourse = ClassCourse::where('id',$course_arrangement_id)->first();

            $semester_id = $ClassCourse->semester_id;
            $semester = Semester::where('id',$semester_id)->first();
            return $semester->name;
        });
        $grid->column('semester_id','学期11')->display(function () {
            $course_arrangement_id = $_GET['id'];
            $ClassCourse = ClassCourse::where('id',$course_arrangement_id)->first();

            $semester_id = $ClassCourse->semester_id;
            $semester = Semester::where('id',$semester_id)->first();
            return $semester->id;
        });
        $grid->student_id('学号');
        $grid->name('学生');
        $grid->column('class.class_name','班级');
        $grid->seat('座位号');

        $grid->column('actions','课程')->display(function () {
            $course_arrangement_id = $_GET['id'];
            $ClassCourse = ClassCourse::where('id',$course_arrangement_id)->first();
            $course_id = $ClassCourse->course_id;

            $course = Course::where('id', $course_id)->first();
            return $course->course_name ;
        });
        $grid->userAction('用户操作')->display(function () use ($course_arrangement_id,$total){
//            $adminPrefix = config('admin.route.prefix');
            $str = "<a class='btn btn-xs fa fa-pencil-square-o grid-check-user-migration' data-course_id=".$course_arrangement_id."  data-total=".$total."  data-student_id=".$this->id."  data-toggle=\"modal\" data-target=\"#myModal3\" >&nbsp;&nbsp;录入</a>";
            return $str;
        });
        $grid->paginate(10);
        $grid->disableCreateButton();
//        $grid->disableActions();
//        $grid->disableFilter();
        $grid->disableRowSelector();
        $grid->disableActions();
        $grid->tools(function ($tools)  {
            $tools->append(new ScoreHandle());
        });
        $grid->filter(function($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            // 在这里添加字段过滤器

            $grades = Grade::get();
            $grade_arr = [];
            foreach ($grades as $grade) {
                $grade_arr[$grade->id] = $grade->class_name;
            }

            $filter->equal('class_id', '班级名称')->select($grade_arr);

            $courses = Course::get();
            $course_arr = [];
            foreach ($courses as $course) {
                $course_arr[$course->id] = $course->course_name;
            }

            $filter->equal('course_id','课程名称')->select($course_arr);

            $filter->equal('end', '是否结课')->radio([
                0    => ' 否',
                1    => ' 是',
            ]);

        });
        return $grid;
    }

    protected function form()
    {
        $form = new Form(new Student);
//        $form->hidden('id');
//        $form->hidden('semester_id');
//        $form->hidden('exam_type');
        $form->test('semester_id');

//        $form->text('ffff1111');
        $form->tools(function (Form\Tools $tools) {
            // 去掉`列表`按钮
//            $tools->disableList();
            // 去掉`删除`按钮
            $tools->disableDelete();
            // 去掉`查看`按钮
            $tools->disableView();
        });
        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
        });

        $form->saving(function ($model)  {
            $student_id = $model->model()->id;
            $semester = Semester::where('is_semester',1)->first();
            $semester_id = $semester->id;
            $exam_type = $model->exam_type;
            $degree = $model->degree;
            $user_id = Admin::user()->id;
            $teacher = Teacher::where('user_id',$user_id)->first();
            $teacher_id = $teacher->id;

            dd($model);
            $score = Score::where([
                'student_id' => $student_id,
                'semester_id' => $semester_id,
                'exam_type' => $exam_type,
                'teacher_id' => $teacher_id
            ])->first();
            if(empty($score)) {
                Score::create([
                    'student_id'    => $student_id,
                    'semester_id'   => $semester_id,
                    'exam_type'     => $exam_type,
                    'teacher_id'    => $teacher_id
                ]);
            }
            if(!empty($degree)) {
                Score::where([
                    'student_id' => $student_id,
                    'semester_id' => $semester_id,
                    'exam_type' => $exam_type,
                    'teacher_id' => $teacher_id
                ])->update(['degree'=>$degree]);
            }
            return;
        });
        return $form;
    }

    public function InsertStudent(Request $request) {
        $input = $request->input();
        $total = $input['total'];
        $course_id = $input['course_id'];
        $student_id = $input['student_id'];
        $html = "
<div class=\"form-group\" >
    <div class='row'>
        <div class=\"col-md-3\" style='text-align: right; '>
            <label for='exam_type' style='padding-top: 3px'>考试类型&nbsp;&nbsp;</label>
        </div>
        <div class=\"col-md-7\">
            <select class='form-control'  name='exam_type' id='exam_type'>
                <option value='第一次月考'>第一次月考</option>
                <option value='第二次月考'>第二次月考</option>
                <option value='期中考试'>期中考试</option>
                <option value='期末考试'>期末考试</option>
            </select>
        </div>
    </div>
</div>
<div class=\"form-group\" >
    <div class='row'>
        <div class=\"col-md-3 \" style='text-align: right;'>
            <label for='degree' style='padding-top: 3px'>成绩&nbsp;&nbsp;</label>
        </div>
        <div class=\"col-md-7\">
            <input  placeholder='请输入分数' class='form-control'type='number' max='$total' min='0'  name='degree' id='degree'>
        </div>
    </div>
</div>
<input  hidden type='text'  id='course_id' value='$course_id'>
<input  hidden type='text'  id='student_id' value='$student_id'>";
        return ['code' => 1,'msg'=>$html];
    }
    public function insertStudentSuccesss(Request $request) {
        $input = $request->input();
        $exam_type = $input['exam_type'];
        $degree = $input['degree'];
        $course_arrange_id = $input['course_id'];
        $student_id = $input['student_id'];

        $course = ClassCourse::where('id',$course_arrange_id)->first();
        $semester = Semester::where('is_semester',1)->first();
        $user_id = Admin::user()->id;
        $teacher = Teacher::where('user_id',$user_id)->first();
        $score = Score::where([ 'student_id'    => $student_id,
            'semester_id'   => $semester->id,
            'exam_type'     => $exam_type])->first();
        if(!empty($score)) {
            return ['code' => 1, 'msg'=>"该学生{$exam_type}成绩，请勿重复提交"];
        }
        $data = [
            'student_id'    => $student_id,
            'semester_id'   => $semester->id,
            'exam_type'     => $exam_type,
            'course_id'     => $course->course_id,
            'degree'        => $degree,
            'teacher_id'    => $teacher->id,
        ];
        $result = Score::create($data);
        if($result) {
            return ['code' => 0, 'msg'=>'录入成功'];
        }else {
            return ['code' => 1,'msg'=> '录入失败'];
        }
    }
}
